package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"vue/models"
)

type VueController struct {
	beego.Controller
}

type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *VueController) Add() {
	name := c.GetString("name")
	address := c.GetString("address")

	if name != "" || address != "" {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "不能为空",
			Data: nil,
		}
		c.ServeJSON()
	}

	vues := models.Vue{
		Name:    name,
		Address: address,
	}

	err := models.DB.Create(&vues).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
}
